import {
	defineStore
} from 'pinia'
import {
	fetchHomeDataApi
} from '../../service/api'
import {
	iscode
} from '../../utils/iscode'
import useCommonStore from '../common/common'


const useHomeStore = defineStore('home', {
	state: () => ({
		homeVideoData: []

	}),
	actions: {
		// - 获取首页随机视频数据
		async fetchHomeVideoAction() {

			const res = await fetchHomeDataApi()
			if (iscode(res)) {
				const {
					list
				} = res.result
				const commonStore = useCommonStore()
				const mapList = list.map((it) => {
					const findItem = commonStore.followHistory.find(item => item.skit_id == it.skit_id)
					if (findItem) {
						
						it.detail[0].isFollow = 1
					} else {
						
						it.detail[0].isFollow = 0
					}


					// 后端的数据格式不太方便，个别参数也添加一下
					const {
						cover_image,
						next_skit_id,
						script_name
					} = it
					it.detail[0].cover_image = cover_image
					it.detail[0].script_name = script_name
					it.detail[0].next_skit_id = next_skit_id

					return it
				})

				this.homeVideoData = [...mapList]
			}
		},
	}
})

export default useHomeStore